package com.ms.service.module.fish.game;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.ms.service.module.fish.MySession;
import com.ms.service.module.fish.utils.GoConstant;
import com.xpec.c4.game.utility.GUID;

public class FishGlobal implements Serializable{

	private static final long serialVersionUID = 1L;
	
	private static Logger logger = LoggerFactory.getLogger(FishGlobal.class);
	
	/**
	 * 在线人数
	 */
	public static AtomicInteger onlineNumber = new AtomicInteger();

	/** key:uid **/
	private static Map<GUID,MySession> userSessionMap = Maps.newConcurrentMap();
	/** key:sid **/
	private static Map<GUID,MySession> sessions = Maps.newConcurrentMap();
	
	public static void addSession(MySession session){
		onlineNumber.incrementAndGet();
		sessions.put(session.getSessionId(),session);
		
		if(logger.isInfoEnabled()){
			logger.info("有新连接加入！ 当前在线人数：{}，s={}", onlineNumber,sessions.size());
		}
	}
	
	public static void removeSession(MySession session){
		if(sessions.remove(session.getSessionId()) != null){
			onlineNumber.decrementAndGet();
		}
		GameUser user = session.getUser();
		if(user != null){
			userSessionMap.remove(user.getId());
			session.removeAttr(GoConstant.USER_SESSION_KEY);
			if(logger.isInfoEnabled()){
				logger.info("断开连接用户：{}-{}", user.getId(),session);
			}
		}
		
		if(logger.isInfoEnabled()){
			logger.info("有连接关闭！ 当前在线人数：s={},u={}", sessions.size(),userSessionMap.size());
		}
	}
	
	public static void addUser(GUID uid,MySession session){
		if(logger.isInfoEnabled()){
			logger.info("登录用户：{}-{}", uid,session);
		}
		userSessionMap.put(uid, session);
		if(logger.isInfoEnabled()){
			logger.info("当前在线人数s={},u={}", sessions.size(),userSessionMap.size());
		}
	}
	
	public static MySession getSessionByUid(GUID uid){
		return userSessionMap.get(uid);
	}
	
	public static MySession getSessionBySid(GUID sid){
		return sessions.get(sid);
	}
	
	public static Collection<MySession> getUserSessions(){
		return userSessionMap.values();
	}

	private static Map<Integer,FishDesk> desks = Maps.newConcurrentMap();
	
	public static FishDesk newDesk(int deskId, int rate) {
		FishDesk desk = new FishDesk(deskId);
		desk.setType((byte) rate);
		desks.put(desk.getId(),desk);
		return desk;
	}

	public static FishDesk getDesk(Integer deskId) {
		FishDesk desk = desks.get(deskId);
		return desk;
	}
	public static Collection<FishDesk> getDesks() {
		return desks.values();
	}
	
	public static AtomicLong sysCurrHave ;
	
	/**
	 * 自动匹配队列
	 */
	private static List<GameUser> robots = Lists.newArrayList();
	private static List<GameUser> readyRobots = Lists.newArrayList();
	
	public static GameUser getRobot(int id){
		GameUser rob = null;
		if(!readyRobots.isEmpty()){
			rob = readyRobots.get(0);
		}else{
//			UsrUser robot = new UsrUser(GoConstant.ROBOT_ID_START + id);
//			robot.setNickName(CommonUtils.genNickName());
//			rob = new GameUser(robot);
//			robots.add(rob);
		}
		return rob;
	}
	public static List<GameUser> getRobots(){
		return robots;
	}
	public static void toReadyRob(List<GameUser> robs){
		robots.removeAll(robs);
		readyRobots.addAll(robs);
	}

}
